package com.leoman.service.impl;

import com.leoman.core.Constant;
import com.leoman.dao.ThirdUserDao;
import com.leoman.entity.ThirdUser;
import com.leoman.service.ThirdUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ThirdUserServiceImpl
 * Created by 涂奕恒 on 2016/11/23 0023 下午 4:01.
 */
@Service
public class ThirdUserServiceImpl implements ThirdUserService {

    @Autowired
    private ThirdUserDao thirdUserDao;

    @Override
    public List<ThirdUser> findAll() {
        return thirdUserDao.findAll();
    }

    @Override
    public Page<ThirdUser> find(int pageNum, int pageSize) {
        return thirdUserDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<ThirdUser> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public ThirdUser getById(int id) {
        return thirdUserDao.findOne(id);
    }

    @Override
    public void deleteById(int id) {
        thirdUserDao.delete(id);
    }

    @Override
    public ThirdUser create(ThirdUser thirdUser) {
        return thirdUserDao.save(thirdUser);
    }

    @Override
    public ThirdUser update(ThirdUser thirdUser) {
        return thirdUserDao.save(thirdUser);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public ThirdUser findOneByUserIdAndType(Integer userId, Integer type) {
        return thirdUserDao.findOneByUserIdAndType(userId, type);
    }
}